package drds.global_transaction.transaction_coordinator;

import drds.global_transaction.api.api.RpcContext;
import drds.global_transaction.api.exception.AbstractExceptionHandler;
import drds.global_transaction.api.exception.GlobalTransactionException;
import drds.global_transaction.api.in_bound_handler.TransactionCoordinatorInboundHandler;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_register.BranchTransactionRegisterRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_register.BranchTransactionRegisterResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_status_report.BranchTransactionStatusReportRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_status_report.BranchTransactionStatusReportResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_begin.GlobalTransactionBeginRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_begin.GlobalTransactionBeginResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.GlobalTransactionStatusReportRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.GlobalTransactionStatusReportResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.global_transaction_commit_status_report.GlobalTransactionCommitRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.global_transaction_commit_status_report.GlobalTransactionCommitResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.global_transaction_rollback_status_report.GlobalTransactionRollbackRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_end_status_report.global_transaction_rollback_status_report.GlobalTransactionRollbackResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_lock_record_ids_check.GlobalTransactionLockRecordIdsCheckRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.global_transaction_lock_record_ids_check.GlobalTransactionLockRecordIdsCheckResponseMessage;

public abstract class AbstractTransactionCoordinatorInboundHandler extends AbstractExceptionHandler implements TransactionCoordinatorInboundHandler
{
	protected abstract void doBranchTransactionRegister(RpcContext rpcContext, //
			BranchTransactionRegisterRequestMessage branchTransactionRegisterRequestMessage, //
			BranchTransactionRegisterResponseMessage branchTransactionRegisterResponseMessage) throws GlobalTransactionException;

	protected abstract void doBranchTransactionReport(RpcContext rpcContext, //
			BranchTransactionStatusReportRequestMessage branchTransactionStatusReportRequestMessage, //
			BranchTransactionStatusReportResponseMessage branchTransactionStatusReportResponseMessage) throws GlobalTransactionException;

	protected abstract void doGlobalTransactionBegin(RpcContext rpcContext, //
			GlobalTransactionBeginRequestMessage globalTransactionBeginRequestMessage, //
			GlobalTransactionBeginResponseMessage globalTransactionBeginResponseMessage) throws GlobalTransactionException;

	protected abstract void doGlobalTransactionCommit(RpcContext rpcContext, //
			GlobalTransactionCommitRequestMessage globalTransactionCommitRequestMessage, //
			GlobalTransactionCommitResponseMessage globalTransactionCommitResponseMessage) throws GlobalTransactionException;

	protected abstract void doGlobalTransactionLockQuery(RpcContext rpcContext, //
			GlobalTransactionLockRecordIdsCheckRequestMessage globalTransactionLockRecordIdsCheckRequestMessage, //
			GlobalTransactionLockRecordIdsCheckResponseMessage globalTransactionLockRecordIdsCheckResponseMessage) throws GlobalTransactionException;

	protected abstract void doGlobalTransactionRollback(RpcContext rpcContext, //
			GlobalTransactionRollbackRequestMessage globalTransactionRollbackRequestMessage, //
			GlobalTransactionRollbackResponseMessage globalTransactionRollbackResponseMessage) throws GlobalTransactionException;

	protected abstract void doGlobalTransactionStatus(RpcContext rpcContext, //
			GlobalTransactionStatusReportRequestMessage globalTransactionStatusReportRequestMessage, //
			GlobalTransactionStatusReportResponseMessage globalTransactionStatusReportResponseMessage) throws GlobalTransactionException;

	@Override
	public BranchTransactionRegisterResponseMessage handle(final RpcContext rpcContext, BranchTransactionRegisterRequestMessage branchTransactionRegisterRequestMessage)
	{
		BranchTransactionRegisterResponseMessage branchTransactionRegisterResponseMessage = new BranchTransactionRegisterResponseMessage();
		exceptionHandleTemplate(new Callback<BranchTransactionRegisterRequestMessage, BranchTransactionRegisterResponseMessage>()
		{
			@Override
			public void execute(BranchTransactionRegisterRequestMessage branchTransactionRegisterRequestMessage, BranchTransactionRegisterResponseMessage branchTransactionRegisterResponseMessage) throws GlobalTransactionException
			{
				doBranchTransactionRegister(rpcContext, branchTransactionRegisterRequestMessage, branchTransactionRegisterResponseMessage);
			}
		}, branchTransactionRegisterRequestMessage, branchTransactionRegisterResponseMessage);
		return branchTransactionRegisterResponseMessage;
	}

	@Override
	public BranchTransactionStatusReportResponseMessage handle(final RpcContext rpcContext, BranchTransactionStatusReportRequestMessage branchTransactionStatusReportRequestMessage)
	{
		BranchTransactionStatusReportResponseMessage branchTransactionStatusReportResponseMessage = new BranchTransactionStatusReportResponseMessage();
		exceptionHandleTemplate(new Callback<BranchTransactionStatusReportRequestMessage, BranchTransactionStatusReportResponseMessage>()
		{
			@Override
			public void execute(BranchTransactionStatusReportRequestMessage branchTransactionStatusReportRequestMessage, BranchTransactionStatusReportResponseMessage branchTransactionStatusReportResponseMessage) throws GlobalTransactionException
			{
				doBranchTransactionReport(rpcContext, branchTransactionStatusReportRequestMessage, branchTransactionStatusReportResponseMessage);
			}
		}, branchTransactionStatusReportRequestMessage, branchTransactionStatusReportResponseMessage);
		return branchTransactionStatusReportResponseMessage;
	}

	@Override
	public GlobalTransactionBeginResponseMessage handle(final RpcContext rpcContext, GlobalTransactionBeginRequestMessage globalTransactionBeginRequestMessage)
	{
		GlobalTransactionBeginResponseMessage globalTransactionBeginResponseMessage = new GlobalTransactionBeginResponseMessage();
		exceptionHandleTemplate(new Callback<GlobalTransactionBeginRequestMessage, GlobalTransactionBeginResponseMessage>()
		{
			@Override
			public void execute(GlobalTransactionBeginRequestMessage globalTransactionBeginRequestMessage, GlobalTransactionBeginResponseMessage globalTransactionBeginResponseMessage) throws GlobalTransactionException
			{
				doGlobalTransactionBegin(rpcContext, globalTransactionBeginRequestMessage, globalTransactionBeginResponseMessage);
			}
		}, globalTransactionBeginRequestMessage, globalTransactionBeginResponseMessage);
		return globalTransactionBeginResponseMessage;
	}

	@Override
	public GlobalTransactionCommitResponseMessage handle(final RpcContext rpcContext, GlobalTransactionCommitRequestMessage globalTransactionCommitRequestMessage)
	{
		GlobalTransactionCommitResponseMessage globalTransactionCommitResponseMessage = new GlobalTransactionCommitResponseMessage();
		exceptionHandleTemplate(new Callback<GlobalTransactionCommitRequestMessage, GlobalTransactionCommitResponseMessage>()
		{
			@Override
			public void execute(GlobalTransactionCommitRequestMessage globalTransactionCommitRequestMessage, GlobalTransactionCommitResponseMessage globalTransactionCommitResponseMessage) throws GlobalTransactionException
			{
				doGlobalTransactionCommit(rpcContext, globalTransactionCommitRequestMessage, globalTransactionCommitResponseMessage);
			}
		}, globalTransactionCommitRequestMessage, globalTransactionCommitResponseMessage);
		return globalTransactionCommitResponseMessage;
	}

	@Override
	public GlobalTransactionLockRecordIdsCheckResponseMessage handle(final RpcContext rpcContext, GlobalTransactionLockRecordIdsCheckRequestMessage globalTransactionLockQueryRequest)
	{
		GlobalTransactionLockRecordIdsCheckResponseMessage globalTransactionLockRecordIdsCheckResponseMessage = new GlobalTransactionLockRecordIdsCheckResponseMessage();
		exceptionHandleTemplate(new Callback<GlobalTransactionLockRecordIdsCheckRequestMessage, GlobalTransactionLockRecordIdsCheckResponseMessage>()
		{
			@Override
			public void execute(GlobalTransactionLockRecordIdsCheckRequestMessage globalTransactionLockRecordIdsCheckRequestMessage, GlobalTransactionLockRecordIdsCheckResponseMessage globalTransactionLockRecordIdsCheckResponseMessage) throws GlobalTransactionException
			{
				doGlobalTransactionLockQuery(rpcContext, globalTransactionLockRecordIdsCheckRequestMessage, globalTransactionLockRecordIdsCheckResponseMessage);
			}
		}, globalTransactionLockQueryRequest, globalTransactionLockRecordIdsCheckResponseMessage);
		return globalTransactionLockRecordIdsCheckResponseMessage;
	}

	@Override
	public GlobalTransactionRollbackResponseMessage handle(final RpcContext rpcContext, GlobalTransactionRollbackRequestMessage globalTransactionRollbackRequestMessage)
	{
		GlobalTransactionRollbackResponseMessage globalTransactionRollbackResponseMessage = new GlobalTransactionRollbackResponseMessage();
		exceptionHandleTemplate(new Callback<GlobalTransactionRollbackRequestMessage, GlobalTransactionRollbackResponseMessage>()
		{
			@Override
			public void execute(GlobalTransactionRollbackRequestMessage globalTransactionRollbackRequestMessage, GlobalTransactionRollbackResponseMessage globalTransactionRollbackResponseMessage) throws GlobalTransactionException
			{
				doGlobalTransactionRollback(rpcContext, globalTransactionRollbackRequestMessage, globalTransactionRollbackResponseMessage);
			}
		}, globalTransactionRollbackRequestMessage, globalTransactionRollbackResponseMessage);
		return globalTransactionRollbackResponseMessage;
	}

	@Override
	public GlobalTransactionStatusReportResponseMessage handle(final RpcContext rpcContext, GlobalTransactionStatusReportRequestMessage globalTransactionStatusRequestMessage)
	{
		GlobalTransactionStatusReportResponseMessage globalTransactionStatusResponseMessage = new GlobalTransactionStatusReportResponseMessage();
		exceptionHandleTemplate(new Callback<GlobalTransactionStatusReportRequestMessage, GlobalTransactionStatusReportResponseMessage>()
		{
			@Override
			public void execute(GlobalTransactionStatusReportRequestMessage globalTransactionStatusReportRequestMessage, GlobalTransactionStatusReportResponseMessage globalTransactionStatusReportResponseMessage) throws GlobalTransactionException
			{
				doGlobalTransactionStatus(rpcContext, globalTransactionStatusReportRequestMessage, globalTransactionStatusReportResponseMessage);
			}
		}, globalTransactionStatusRequestMessage, globalTransactionStatusResponseMessage);
		return globalTransactionStatusResponseMessage;
	}

}
